package com.wms_backend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wms_backend.dao.InboundDao;
import com.wms_backend.dao.InventoryDao;


import com.wms_backend.dao.UserDao;

import com.wms_backend.entity.Result;
import com.wms_backend.service.InventoryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


import java.util.*;


@Service
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    private InventoryDao inventoryDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    InboundDao inboundDao;

    @Override
    public Page<List<Map<String, Object>>> selectExisting(String userId, JSONObject param, Pageable pageable) {

        String name = param.getString("name");
        String kind = param.getString("kind");
        String warehouse = param.getString("warehouse");//字符串是空字符串，不是null
        String createUser = param.getString("createUser");
        Integer unit = param.getInteger("unit");
        Integer number = param.getInteger("number");

        //   System.out.println(userId);
     //   System.out.println(userId);
        Integer company = userDao.findCompanyIdByUserId(userId).getCompanyId();

        Page<List<Map<String, Object>>> pi = inventoryDao.findBySixParam(name.toLowerCase(), kind.toLowerCase(), unit, number, warehouse, company, createUser, pageable);

        return pi;


    }

    @Override
    public List<Map<String, Object>> getRepositoryStatus(JSONObject param) {
        String repositoryName = param.getString("repository");
        return inventoryDao.getRepositoryStatus(repositoryName);

    }

    @Override
    public List<Map<String, Object>> getShelfGoods(JSONObject param,String userId) {
  String repositoryName = param.getString("repository");
  Integer shelfOrder = param.getInteger("shelfOrder");
  //此Map不可修改
   List<Map<String,Object>>  goods= inventoryDao.getShelfGoods(repositoryName,shelfOrder);

        Integer companyId = userDao.findCompanyIdByUserId(userId).getCompanyId();

        List<Map<String, Object>> ans = new ArrayList<>();


        for (Map<String, Object> map : goods) {
            Map<String, Object> mutableMap = new HashMap<>(map);//不能放在外面
            ans.add(mutableMap);
        }

       for (Map<String, Object> element : ans) {

            if(element.get("status").equals("3")&&!(companyId==element.get("companyId"))){
   //     System.out.println("这里来过");
         element.put("name","其他公司货物");

            }


        }

 return ans;
    }



    @Override
    public Result getCompanyId(String userId){
        Integer companyId = userDao.findCompanyIdByUserId(userId).getCompanyId();
      return  Result.success(companyId);
    }
}
